import unittest

import requests
from parameterized import parameterized

from api.RegLoginApi import RegLoginApi
from api.recharge_api import RechargeApi
from common import utils_p2p


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 实例化登录模块封装的类
        self.reg_login_api = RegLoginApi(self.session)
        # 实例化充值模块封装的类
        self.reg_recharge_api = RechargeApi(self.session)
        # 登录
        self.reg_login_api.user_login("13422221111", "123abc")

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 获取充值验证码测试用例
    @parameterized.expand(utils_p2p.read_json("recharge_data", "recharge_verify_code"))
    def test01_get_recharge_verify_code(self, r, esp_status_code):
        # 获取充值图片验证码
        res = self.reg_recharge_api.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(esp_status_code, res.status_code)

    # 充值测试用例
    @parameterized.expand(utils_p2p.read_json("recharge_data", "recharge"))
    def test02_recharge(self, money, code, esp_status_code, esp_status, exp_description):
        # 获取充值图片验证码
        self.reg_recharge_api.get_recharge_verify_code(12312345)
        # 充值
        res = self.reg_recharge_api.recharge(amount_v=money, valicode_v=code)
        # 断言
        # 公共断言
        utils_p2p.common_assert(self, res, esp_status_code, esp_status, exp_description)
        self.assertEqual(esp_status_code, res.status_code)
        self.assertEqual(esp_status, res.json().get("status"))

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 获取充值图片验证码
        self.reg_recharge_api.get_recharge_verify_code(12312345)
        # 充值
        res = self.reg_recharge_api.recharge(amount_v="100000", valicode_v="8888")
        request_data = utils_p2p.html_util(res)
        # 获取第三方开户接口的url
        url = request_data[0]
        # 获取第三方开户接口的请求体数据
        from_dict = request_data[1]
        # 获取第三方接口返回的响应结果
        res_3_recharge = self.reg_recharge_api.third_recharge(url, from_dict)
        # 断言
        self.assertEqual(200, res_3_recharge.status_code)
        self.assertIn("OK", res_3_recharge.text)
